<template>
  <div style="display: flex;height: 85vh;">
    <div class="solveName-box">
      <el-table :row-class-name="rowStyle" border @row-click="handleClick" highlight-current-row :data="solveNameList"
        @selection-change="handleSelectionChange" :header-cell-style="{
  background: '#ebebeb'
}">
        <el-table-column label="序号" align="center" type="index" />
        <af-table-column label="解读表格类型名称" align="center" prop="name" />
      </el-table>

    </div>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="100px">
        <el-form-item label="配置表头名称" prop="name">
          <el-input v-model="queryParams.name" placeholder="请输入配置表头名称" clearable style="width: 240px"
            @keyup.enter.native="handleQuery" />
        </el-form-item>

        <el-form-item label="状态" prop="status" style="margin-left: -30px">
          <el-select v-model="queryParams.status" placeholder="全部" clearable style="width: 240px">
            <el-option value="" label="全部"></el-option>
            <el-option v-for="item in statusType" :key="item.status" :label="item.title" :value="item.status" />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8" type="flex" justify="space-between">
        <el-col :span="1.5">
          <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['system:dict:add']">
            新增
          </el-button>

        </el-col>
        <el-col :span="1.5">
          <el-dropdown trigger="click">
            <el-button type="primary" size="mini">
              导出<i class="el-icon-arrow-down el-icon--right"></i>
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="handleExport('now')">导出当前</el-dropdown-item>
              <el-dropdown-item @click.native="handleExport('all')">导出全部</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
      <el-table :row-class-name="rowStyle" border v-loading="loading" highlight-current-row
        :data="originalConfigurationList" @selection-change="handleSelectionChange" height="calc(100vh - 250px)"
        :header-cell-style="{
  background: '#ebebeb'
}">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" align="center" type="index" />
        <af-table-column label="配置表头名称" align="center" prop="name" />
        <af-table-column label="顺序号" align="center" prop="sortNum" />
        <af-table-column label="是否显示配置表头" align="center" prop="analyzeTableStatus" width="150">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.analyzeTableStatus == 1" active-color="#169bd5" inactive-color="#aaaaaa"
              @change="switchShowStatus2(scope.row)" v-if="fn(scope.row)"
               />
              
          </template>
        </af-table-column>
        <af-table-column label="状态" align="center" prop="status" width="150">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.status == 1" active-color="#169bd5" inactive-color="#aaaaaa"
              @change="switchShowStatus(scope.row)"
              v-if="fn(scope.row)" />
          </template>
        </af-table-column>


        <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width" width="150">
          <template slot-scope="scope"
            v-if="fn(scope.row)">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['system:dict:edit']">编辑</el-button>
            <el-button v-if="scope.row.status === 0" size="mini" style="color: #FA5555;" type="text"
              icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['system:dict:remove']">删除</el-button>
            <el-button v-else size="mini" style="color: #999999;" type="text" icon="el-icon-delete" disabled>删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <div>
        <pagination style="margin:0;padding: 0;" :total="total" :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize" @pagination="getList" />
      </div>


      <!-- 添加或修改参数配置对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
          <el-form-item label="配置表头名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入配置表头名称" />
          </el-form-item>
          <el-form-item label="顺序号" prop="sort">
            <el-input v-model="form.sortNum" placeholder="请输入顺序号" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="submitForm">确 定</el-button>

        </div>
      </el-dialog>

    </div>
  </div>
</template>

<script>
import { originalConfiguration, originalConfigurationUpdate, originalConfigurationEdit, originalConfigurationAdd, originalConfigurationDel, originalConfigurationExport } from "@/api/dictionary/interpretationDictionary/originalConfiguration"
import { enumerate } from '@/api/dictionary/systemSetting/enumeration'
import { solveName } from '@/api/dictionary/interpretationDictionary/solveDictionary'

export default {
  name: "originalConfiguration",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 字典表格数据
      originalConfigurationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        name: undefined,
        status: '',
        dictMicrobialTableId: 1
      },
      statusType: [
        {
          title: '开启',
          status: 1
        },
        {
          title: '禁用',
          status: 0
        }
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "请输入配置表头名称", trigger: "blur" }
        ]
      },
      solveNameList: []
    }
  },
  created() {
    this.getNameList()
  },
  methods: {
    fn(row){
      if(this.queryParams.dictMicrobialTableId===1){
        if(row.name != 'taxID' && row.name != 'Genus' && row.name != 'GenusChinese' && row.name != 'Species' && row.name != 'SpeciesChinese'){
          return true
        }
      }
      if(this.queryParams.dictMicrobialTableId===2){
        if(row.name != 'Species' && row.name != 'Gene'&& row.name != 'Description'&& row.name != 'reads'){
          return true
        }
      }
    },
    /** 查询微生物原始表配置 */
    getList() {
      this.loading = true
      originalConfiguration(this.queryParams).then(response => {
        this.originalConfigurationList = response.rows
        this.total = response.total
        this.loading = false
      }
      )
    },
    getNameList() {
      solveName().then(response => {
        this.solveNameList = response.rows
        this.loading = false
      })
    },
    handleClick(row) {
      this.queryParams.dictMicrobialTableId = row.id
      console.log(this.queryParams.dictMicrobialTableId );
      this.form.dictMicrobialTableId = row.id
      this.getList()
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "微生物原始表配置：新增"
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      originalConfigurationEdit(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "微生物原始表配置：编辑"
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            this.form.dictMicrobialTableId = this.queryParams.dictMicrobialTableId
            originalConfigurationUpdate(this.form).then(response => {
              this.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            this.form.status = 0
            this.form.analyzeTableStatus=0
            this.form.dictMicrobialTableId = this.queryParams.dictMicrobialTableId
            originalConfigurationAdd(this.form).then(response => {
              this.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      // const id = row.id || this.ids
      console.log(row);
      let data = [{
        name: row.name,
        dictMicrobialTableId: this.queryParams.dictMicrobialTableId
      }]
      // console.log(JSON.stringify(data));
      // return
      this.$confirm('是否确认删除选择的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return originalConfigurationDel(data)
      }).then(() => {
        this.getList()
        this.msgSuccess("删除成功")
      })
    },
    // /** 导出按钮操作 */
    handleExport(e) {
      var different = ''
      var params = ''
      if (e === 'now') {
        different = '当前'
        params = this.queryParams //对应接口参数
      } else if (e === 'all') {
        different = '所有'
        params = null
      } else {
        return
      }
      let that = this
      let loading
      this.$confirm('是否确认导出' + different + '数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        loading = that.$loading({
          lock: true,
          text: '导出中',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        })
        originalConfiguration(params).then((response) => {
          let mydata = [
            [
              '序号',
              '配置表头名称',
              '顺序号',
              '状态',
            ],
            ...response.rows.map((item, index) => ([
              index + 1,
              item.name,
              item.sortNum,
              item.status === 0 ? '禁用' : '开启',
            ]))
          ]
          let requireMerge = []
          that.$util.createxlsx({
            mydata,
            fileName: '微生物原始表配置',
            dataList: response.rows,
            type: 'orderId',
            requireMerge,
            loading
          })
        })
      }).catch(function () { })
    },
    //切换状态
    switchShowStatus(row) {

      if (row.status === 1) {

        this.handleUnPublish(row)
      } else if (row.status === 0) {

        this.handlePublish(row)
      }
    },
    switchShowStatus2(row) {

      if (row.analyzeTableStatus === 1) {

        this.handleUnPublish2(row)
      } else if (row.analyzeTableStatus === 0) {

        this.handlePublish2(row)
      }
    },
    handlePublish(row) {
      let that = this
      this.$confirm('是否开启', "确认", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {

        return originalConfigurationUpdate({
          ...row,
          status: 1,
          dictMicrobialTableId: this.queryParams.dictMicrobialTableId
        })
      }).then(() => {

        this.getList()
        this.msgSuccess("开启成功")
      }).catch(function () {
      })
    },
    handlePublish2(row) {
      let that = this
      this.$confirm('是否开启', "确认", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {

        return originalConfigurationUpdate({
          ...row,
          analyzeTableStatus: 1,
          dictMicrobialTableId: this.queryParams.dictMicrobialTableId
        })
      }).then(() => {

        this.getList()
        this.msgSuccess("开启成功")
      }).catch(function () {
      })
    },


    handleUnPublish(row) {
      this.$confirm('是否禁用', "确认", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        return originalConfigurationUpdate({
          ...row,
          status: 0,
          dictMicrobialTableId: this.queryParams.dictMicrobialTableId
        })
      }).then(() => {
        this.getList()
        this.msgSuccess("禁用成功")
      }).catch(function () {
      })
    },
    handleUnPublish2(row) {
      this.$confirm('是否禁用', "确认", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        return originalConfigurationUpdate({
          ...row,
          analyzeTableStatus: 0,
          dictMicrobialTableId: this.queryParams.dictMicrobialTableId
        })
      }).then(() => {
        this.getList()
        this.msgSuccess("禁用成功")
      }).catch(function () {
      })
    },

    rowStyle({ row }) {
      var arr = this.ids
      for (let i = 0; i < arr.length; i++) {
        if (row.id === arr[i]) {
          return "rowStyle"
        }
      }
    },
  }
}
</script>
<style scoped>
.solveName-box {
  width: 30%;
  padding: 20px;
  height: 100%;
  border-right: 1px solid #dfe6ec;
}

.app-container {
  width: 70%;
  height: 100%;
}

::v-deep .el-table__body tr.current-row>td {

  background: #a8d6fe !important;
}

::v-deep .rowStyle {
  background-color: #a8d6fe !important;
}

::v-deep .el-table__body tr:hover>td {
  background: #a8d6fe !important;
}
</style>
